<template>
  <el-dialog
    :title="!dataForm.gsmSeckillSessionId ? '添加场次' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="场次名称" prop="gsmSeckillSessionName">
      <el-input v-model="dataForm.gsmSeckillSessionName" placeholder="场次名称"></el-input>
    </el-form-item>
    <el-form-item label="每日开启时间" prop="gsmSeckillSessionStartTime">
      <el-date-picker
        v-model="dataForm.gsmSeckillSessionStartTime"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="每日开启时间">
      </el-date-picker>
      至
      <el-date-picker
        v-model="dataForm.gsmSeckillSessionEndTime"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="每日结束时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="" prop="gsmSeckillSessionFreight">
      <el-switch
        v-model="dataForm.gsmSeckillSessionFreight"
        active-color="#13ce66"
        inactive-color="#ff4949"
        active-value="0"
        inactive-value="1"
        active-text="包邮"
        inactive-text="不包邮">
      </el-switch>
    </el-form-item>
    <el-form-item label="" prop="gsmSeckillSessionBonus">
      <el-switch
        v-model="dataForm.gsmSeckillSessionBonus"
        active-color="#13ce66"
        inactive-color="#ff4949"
        active-value="0"
        inactive-value="1"
        active-text="有返金"
        inactive-text="无返金">
      </el-switch>
    </el-form-item>
    <el-form-item label="秒杀仓库" prop="gsmSeckillSessionStock">
      <el-input v-model="dataForm.gsmSeckillSessionStock" placeholder="秒杀仓库"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="gsmSeckillSessionLimitCount">
      <el-input v-model="dataForm.gsmSeckillSessionLimitCount" placeholder="限购数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gsmSeckillSessionId: 0,
          gsmSeckillSessionName: '',
          gsmSeckillSessionStartTime: '',
          gsmSeckillSessionEndTime: '',
          gsmSeckillSessionStatus: '',
          gsmSeckillSessionCreateTime: '',
          gsmSeckillSessionFreight: '',
          gsmSeckillSessionBonus: '',
          gsmSeckillSessionStock: '',
          gsmSeckillSessionLimitCount: '',
        },
        dataRule: {
          gsmSeckillSessionName: [
            { required: true, message: '场次名称不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionStartTime: [
            { required: true, message: '每日开启时间不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionEndTime: [
            { required: true, message: '每日结束时间不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionStatus: [
            { required: true, message: '启用状态不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionFreight: [
            { required: true, message: '是否包邮不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionBonus: [
            { required: true, message: '是否由返金不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionStock: [
            { required: true, message: '秒杀仓库不能为空', trigger: 'blur' }
          ],
          gsmSeckillSessionLimitCount: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gsmSeckillSessionId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmSeckillSessionId) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/seckillsession/info/${this.dataForm.gsmSeckillSessionId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gsmSeckillSessionName = data.seckillSession.gsmSeckillSessionName
                this.dataForm.gsmSeckillSessionStartTime = data.seckillSession.gsmSeckillSessionStartTime
                this.dataForm.gsmSeckillSessionEndTime = data.seckillSession.gsmSeckillSessionEndTime
                this.dataForm.gsmSeckillSessionStatus = data.seckillSession.gsmSeckillSessionStatus
                this.dataForm.gsmSeckillSessionCreateTime = data.seckillSession.gsmSeckillSessionCreateTime
                this.dataForm.gsmSeckillSessionFreight = data.seckillSession.gsmSeckillSessionFreight
                this.dataForm.gsmSeckillSessionBonus = data.seckillSession.gsmSeckillSessionBonus
                this.dataForm.gsmSeckillSessionStock = data.seckillSession.gsmSeckillSessionStock
                this.dataForm.gsmSeckillSessionLimitCount = data.seckillSession.gsmSeckillSessionLimitCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/seckillsession/${!this.dataForm.gsmSeckillSessionId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmSeckillSessionId': this.dataForm.gsmSeckillSessionId || undefined,
                'gsmSeckillSessionName': this.dataForm.gsmSeckillSessionName,
                'gsmSeckillSessionStartTime': this.dataForm.gsmSeckillSessionStartTime,
                'gsmSeckillSessionEndTime': this.dataForm.gsmSeckillSessionEndTime,
                'gsmSeckillSessionStatus': this.dataForm.gsmSeckillSessionStatus,
                'gsmSeckillSessionCreateTime': this.dataForm.gsmSeckillSessionCreateTime,
                'gsmSeckillSessionFreight': this.dataForm.gsmSeckillSessionFreight,
                'gsmSeckillSessionBonus': this.dataForm.gsmSeckillSessionBonus,
                'gsmSeckillSessionStock': this.dataForm.gsmSeckillSessionStock,
                'gsmSeckillSessionLimitCount': this.dataForm.gsmSeckillSessionLimitCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
